let name = document.querySelector('#login-name');
let pwd = document.querySelector('#login-pwd');
let p = document.getElementsByClassName('xp')[0];
let p1 = document.getElementsByClassName('xp1')[0];
let u1 = document.querySelector('#u1');
let u2 = document.querySelector('#u2');
let bname = document.querySelector('#bname');
let userclose = document.querySelector('#userclose');
let yz = document.querySelector('#yz');



let loginbtn = document.querySelector('#login-btn').onclick = () => {

    let username = name.value;
    let password = pwd.value;
    utils.fetch("./api/v1/user/login.php", { username, password }).then((resp) => {
        if (resp.code === 200) {


            let info = { username, password }
            p1.classList.remove('hidden')
            u1.classList.add('hidden')
            u2.classList.remove('hidden')
            bname.innerHTML = username;
            console.log(111);
            setTimeout(() => {
                $('#loginModal').modal('hide')
            }, 1000)


            if (yz.checked == true) {

                utils.setCookie('user', JSON.stringify(info), { expires: 7, path: "/" })

                // username.value = info.username;
                // console.log(info.username);

            } else {
                let objs = utils.getCookie("user");
                if (objs) {
                    utils.setCookie('user', JSON.stringify(info), { expires: -1, path: "/" })
                }
            }
        } else {
            p.classList.remove('hidden')
            setTimeout(() => {
                p.classList.add('hidden')
            }, 2000)

        }
    })

}
$('#loginModal').on('hidden.bs.modal', function(e) {
    name.value = pwd.value = ''
})
userclose.onclick = function() {
    u2.classList.add('hidden')
    u1.classList.remove('hidden')
    utils.setCookie('user', JSON.stringify(info), { expires: -7, path: "/" })
}
let zybtn = document.querySelector('#zybtn').onclick = () => {
    yz.checked = false
    let obj = utils.getCookie("user");
    if (obj) {
        obj = JSON.parse(obj);
        name.value = obj.username;
        pwd.value = obj.password;
    }


}